
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Javascript RSA - Login Test</title>

<SCRIPT src="javascripts/pidcrypt.js"></SCRIPT>
<SCRIPT src="javascripts/pidcrypt_util.js"></SCRIPT>
<SCRIPT src="javascripts/asn1.js"></SCRIPT>
<!--needed for parsing decrypted rsa certificate-->
<SCRIPT src="javascripts/jsbn.js"></SCRIPT>
<!--needed for rsa math operations-->
<SCRIPT src="javascripts/rng.js"></SCRIPT>
<!--needed for rsa key generation-->
<SCRIPT src="javascripts/prng4.js"></SCRIPT>
<!--needed for rsa key generation-->
<SCRIPT src="javascripts/rsa.js"></SCRIPT>
<!--needed for rsa en-/decryption-->


<script src="jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="jsbn.js"></script>
<script type="text/javascript" src="rsa.js"></script>
<script type="text/javascript" src="sha1.js"></script>
<script type="text/javascript" src="form2object.js"></script>
<script type="text/javascript" src="json2.js"></script>
<script type="text/javascript">
	var $pem = "";
	var $key = "";
	var categories = "";
	var countUser = 1;
	var pubkeys = new Array();

	function edit(id) {
		var t = $('#passwordID');
		document.getElementById("passwordID").value = id;
		$.ajax({
			url : '../passwords/password/' + id,
			type : 'GET',
			dataType : 'json',
			contentType : "application/json; charset=utf-8",
			success : function(result) {
				document.getElementById("title").value = result.title;
				document.getElementById("username").value = result.username;
				document.getElementById("notes").value = result.notes;
				$('#selectedUsers').empty();
				$.each(result.passwordUserHashs, function() {
					if (this.user.id != $('#ownUserID').val()) {
						addUserByUserID(this.user.username, this.user.id);
					}
				});
			}
		});
		$('#edit').show();
	}
	$(document).ready(
			function() {
				$('#add').click(function() {
					addUser();
				});
				$('#remove').click(function() {
					removeUser();
				});

				$.ajax({
					url : '../login/current_user',
					type : 'GET',
					async : false,
					dataType : 'json',
					contentType : "application/json; charset=utf-8",
					success : function(result) {
						$('#ownUserID').val(result.id);
						$pem = result.pubkey;
						$key = RSA.getPublicKey($pem);
					}
				});
				$.ajax({
					url : '../categories/read',
					type : 'GET',
					async : false,
					dataType : 'json',
					contentType : "application/json; charset=utf-8",
					success : function(result) {
						var options = $("#categories");
						$.each(result, function() {

							options.append($("<option />").val(this.id).text(
									this.passwordCategoryName));

						});

						$.ajax({
							url : '../users/read',
							type : 'GET',
							async : false,
							dataType : 'json',
							contentType : "application/json; charset=utf-8",
							success : function(result) {
								var options = $("#availableUsers");
								$.each(result, function() {
									if (this.id != $('#ownUserID').val()) {
										options.append($("<option />").val(
												this.id).text(this.username));
										pubkeys['' + this.id] = this.pubkey;
									}
								});
							}
						});
					}
				});
				
			});
	function addUser() {
		var hidden = $('#hidden');
		var username = $('#availableUsers').val();
		var username = $("#availableUsers option:selected").text();
		var userID = $('#availableUsers').val();
		addUserByUserID(username, userID);
	}

	function addUserByUserID(username, userID) {
		$('#selectedUsers').append($("<option />").val(userID).text(username));
		$("#availableUsers option[value='" + userID + "']").remove();
		/* $("#availableUsers").select(userID);
		$("#availableUsers option:selected").remove(); */
	}

	function removeUser() {
		$('#availableUsers').append(
				$("<option />").val($("#selectedUsers option:selected").val())
						.text($("#selectedUsers option:selected").text()));
		$("#selectedUsers option:selected").remove();
	}

	function assemble() {
		var password_field = document.getElementById("password");
		var data_field = document.getElementById("data");
		data_field.value = sha1(password_field.value) + (new Date().getTime());
	}

	function encryptNew(toEncrypt, pubkey) {
		params = certParser(pubkey);
		var key = pidCryptUtil.decodeBase64(params.b64);
		//new RSA instance
		var rsa = new pidCrypt.RSA();
		//RSA encryption
		//ASN1 parsing
		var asn = pidCrypt.ASN1.decode(pidCryptUtil.toByteArray(key));
		var tree = asn.toHexTree();
		//setting the public key for encryption
		rsa.setPublicKeyFromASN(tree);
		var t = new Date(); // timer
		crypted = rsa.encrypt(toEncrypt);
		return pidCryptUtil.fragment(pidCryptUtil.encodeBase64(pidCryptUtil
				.convertFromHex(crypted)), 64);
	}

	function encrypt() {
		var data_field = document.getElementById("password2Hash");
		$('#ownHash').val(encryptNew(data_field.value, $pem));
		var i = 1;
		$("#selectedUsers > option")
				.each(
						function() {
							var data_field = document
									.getElementById("password2Hash");
							var hidden = $('#hidden');
							var username = $('#availableUsers').val();
							hidden
									.append('<input id="userID_' + i + '" name="passwordUserHashs[' + i + '].userID" type="text" value="' + this.value +'">');
							var pubkey = pubkeys['' + this.value];
							var pem = RSA.getPublicKey(pubkey);
							var hashOfUser = encryptNew(data_field.value,
									pubkey);
							hidden
									.append('<input id="hash_' + i + '" name="passwordUserHashs[' + i + '].hash" type="text" value="' + hashOfUser +'">');
							i++;
						});
	}
	function submitData() {

		encrypt();
		var formData = form2object('passwordForm', '.', true, function(node) {
			/* if (node.id && node.id.match(/callbackTest/)) {
				return {
					name : node.id,
					value : node.innerHTML
				};
			} */
		});
		$.ajax({
			url : '../passwords/create',
			type : 'POST',
			data : JSON.stringify(formData, null, '\t'),
			dataType : 'json',
			contentType : "application/json; charset=utf-8",
			success : function(result) {
				alert('Created Password successfully');
				$('#content').load('passwords.html');
			}
		});
	}
</script>

</head>

<body>
	<h1>Create Password</h1>
	<form id="passwordForm" action="javascript:submitData()">
		<table>
			<tr>
				<td>ID</td>
				<td><input type="text" name="id" id="passwordID" /></td>
			</tr>
			<tr>
				<td>Title:</td>
				<td><input type="text" name="title" id="title" /></td>
			</tr>
			<tr>
				<td>Username:</td>
				<td><input type="text" name="username" id="username" /></td>

			</tr>
			<tr>
				<td>Password:</td>
				<td><input type="text" id="password2Hash" /></td>
			</tr>
			<tr>
				<td>Category:</td>
				<td><select id="categories" type="text"
					name="passwordCategoryID" value="1"></select>
				</td>
			</tr>
			<tr>
				<td>Notes</td>
				<td><textarea name="notes" id="notes"></textarea></td>
			</tr>
			<tr>
				<td>Your UserID:</td>
				<td><input id="ownUserID" type="text"
					name="passwordUserHashs[0].userID"></input>
				</td>
			</tr>
			<tr>
				<td>Your Hash:</td>
				<td><textarea id="ownHash" name="passwordUserHashs[0].hash"></textarea>
				</td>
			</tr>
			<tr>
				<td>Available Users:</td>
				<td><select id="availableUsers"></select><input type="Button"
					id="add" value="+"><input id="remove" type="Button"
					value="-"></td>
			<tr>
				<td>Selected Users:</td>
				<td><select id="selectedUsers" multiple="multiple"></select>
				</td>
			</tr>
			<tr>
				<td>Hidden</td>
				<td>
					<div id="hidden" /></td>
			</tr>
			<tr>
				<td />
				<td><input type="submit" /></td>
			</tr>
		</table>

	</form>
</body>

</html>
